<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="700px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="150px">

          <el-form-item label="报名会员" prop="yxUserId">
            <el-input
              v-if="false"
              v-model="form.yxUserId"
              style="width: 370px;"
            />
            <div v-if="form.yxUserId">
              {{ dialogTableDataYxUserId.showInputValue ? dialogTableDataYxUserId.showInputValue : form.username }}
            </div>
            <!-- 新增组件弹出式表单 -->
            <dialogTable
              :dialogTableData.sync="dialogTableDataYxUserId"
              @select-callback="dialogTableSelectedCallBackYxUserId"
            ></dialogTable>
          </el-form-item>

          <el-form-item label="活动" prop="activityId">
            <el-input
              v-if="false"
              v-model="form.activityId"
              style="width: 370px;"
            />
            <div v-if="form.activityId">
              {{ dialogTableDataActivityId.showInputValue ? dialogTableDataActivityId.showInputValue : form.activityTitle }}
            </div>
            <!-- 新增组件弹出式表单 -->
            <dialogTable
              :dialogTableData.sync="dialogTableDataActivityId"
              @select-callback="dialogTableSelectedCallBackActivityId"
            ></dialogTable>
          </el-form-item>

          <el-form-item label="活动" prop="activityTeamId">
            <el-input
              v-if="false"
              v-model="form.activityTeamId"
              style="width: 370px;"
            />
            <div v-if="form.activityTeamId">
              {{ dialogTableDataActivityTeamId.showInputValue ? dialogTableDataActivityTeamId.showInputValue : form.name }}
            </div>
            <!-- 新增组件弹出式表单 -->
            <dialogTable
              :dialogTableData.sync="dialogTableDataActivityTeamId"
              @select-callback="dialogTableSelectedCallBackActivityTeamId"
            ></dialogTable>
          </el-form-item>


          <el-form-item label="状态" prop="applyStatus">
            <el-select v-model="form.applyStatus" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.ACTIVITY_TEAM_APPLY_STATUS"
                :key="item.id"
                :label="item.label"
                :value="item.value" />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('username')" prop="username" label="报名会员" />
        <el-table-column v-if="columns.visible('activityTitle')" prop="activityTitle" label="活动" />
        <el-table-column v-if="columns.visible('activityCode')" prop="activityCode" label="活动编码" />
        <el-table-column v-if="columns.visible('name')" prop="name" label="小组负责人" />
        <el-table-column v-if="columns.visible('studentPhone')" prop="studentPhone" label="小组负责人电话" />
        <el-table-column v-if="columns.visible('applyStatus')" prop="applyStatus" label="状态">
          <template slot-scope="scope">
            {{ dict.label.ACTIVITY_TEAM_APPLY_STATUS[scope.row.applyStatus] }}
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('orderNum')" prop="orderNum" label="订单编号" />
        <el-table-column v-if="columns.visible('createTime')" prop="createTime" label="报名时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-permission="['admin','tanzerActivityTeamApply:edit','tanzerActivityTeamApply:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudTanzerActivityTeamApply from '@/api/tanzer/tanzerActivityTeamApply'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import MaterialList from "@/components/material";
import dialogTable from "@/views/components/dialogTable/index.vue";

// crud交由presenter持有
const defaultCrud = CRUD({ title: '小组报名信息', url: 'api/tanzerActivityTeamApply', sort: 'id,desc', crudMethod: { ...crudTanzerActivityTeamApply }})
const defaultForm = { id: null, yxUserId: null, activityId: null, activityTeamId: null, applyStatus: null, orderNum: null, isDel: null, createTime: null, createBy: null, updateBy: null, updateTime: null }
export default {
  name: 'TanzerActivityTeamApply',
  components: {dialogTable, pagination, crudOperation, rrOperation, udOperation ,MaterialList},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  dicts: ['ACTIVITY_TEAM_APPLY_STATUS'],
  data() {
    return {

      permission: {
        add: ['admin', 'tanzerActivityTeamApply:add'],
        edit: ['admin', 'tanzerActivityTeamApply:edit'],
        del: ['admin', 'tanzerActivityTeamApply:del']
      },
      rules: {
        yxUserId: [
          { required: true, message: '会员ID不能为空', trigger: 'blur' }
        ],
        activityId: [
          { required: true, message: '活动id不能为空', trigger: 'blur' }
        ],
        activityTeamId: [
          { required: true, message: '小组ID不能为空', trigger: 'blur' }
        ],
        applyStatus: [
          { required: true, message: '状态不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'id', display_name: '表ID，主键，供其他表做外键' },
        { key: 'yxUserId', display_name: '会员ID' },
        { key: 'activityId', display_name: '活动id' },
        { key: 'activityTeamId', display_name: '小组ID' },
        { key: 'applyStatus', display_name: '状态' },
        { key: 'orderNum', display_name: '订单编号' }
      ],

      dialogTableDataActivityId: {
        title: "活动",
        dialogVisible: false,
        url: "api/tanzerActivity",
        sort: "id,desc",
        buttonContext: "选择活动",
        showInputValue: "",
        queryTypeOptions: [
          { key: 'activityTitle', display_name: '名称' },
          { key: 'activityCode', display_name: '编码' },
        ],
        elTtableColumns: [
          {
            columnProp: "activityTitle",
            columnLabel: "名称"
          },
          {
            columnProp: "activityCode",
            columnLabel: "编码"
          },
          {
            columnProp: "activityStatus",
            columnLabel: "状态"
          },
        ]
      },
      dialogTableDataYxUserId: {
        title: "会员列表",
        dialogVisible: false,
        url: "/api/yxUser",
        sort: "uid,desc",
        buttonContext: "请选择会员",
        showInputValue: "",
        queryTypeOptions: [
          { key: "username", display_name: "用户名称" },
          { key: "phone", display_name: "手机号码" }
        ],
        elTtableColumns: [
          {
            columnProp: "username",
            columnLabel: "用户名称"
          },
          {
            columnProp: "nickname",
            columnLabel: "用户昵称"
          },
          {
            columnProp: "phone",
            columnLabel: "手机号码"
          },
        ]
      },
      dialogTableDataActivityTeamId: {
        title: "活动小组",
        dialogVisible: false,
        url: "api/tanzerActivityTeam",
        sort: "id,desc",
        buttonContext: "选择活动小组",
        showInputValue: "",
        queryTypeOptions: [
          { key: 'name', display_name: '小组负责人' },
          { key: 'studentPhone', display_name: '电话' },
          { key: 'studentSex', display_name: '性别' },
        ],
        elTtableColumns: [
          {
            columnProp: "name",
            columnLabel: "小组负责人"
          },
          {
            columnProp: "studentPhone",
            columnLabel: "电话"
          },
          {
            columnProp: "studentSex",
            columnLabel: "性别"
          },
        ]
      },


    }
  },
  watch: {
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }else{
        delete this.crud.params.id
        delete this.crud.params.yxUserId
        delete this.crud.params.activityId
        delete this.crud.params.activityTeamId
        delete this.crud.params.applyStatus
        delete this.crud.params.orderNum
      }
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
    },

    dialogTableSelectedCallBackActivityId(row) {
      this.form.activityId = row.id;
      if (this.form.activityId) {
        this.dialogTableDataActivityId.showInputValue = row.activityTitle;
      }
    },
    dialogTableSelectedCallBackYxUserId(row) {
      this.form.yxUserId = row.uid;
      if (this.form.yxUserId) {
        this.dialogTableDataYxUserId.showInputValue = row.username;
      }
    },
    dialogTableSelectedCallBackActivityTeamId(row) {
      this.form.activityTeamId = row.id;
      if (this.form.activityTeamId) {
        this.dialogTableDataActivityTeamId.showInputValue = row.name;
      }
    },
  }
}



</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
</style>
